package com.battle.service.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.battle.pojo.vo.AliyunCredentials;
import com.battle.service.config.IceConfig;
import com.battle.service.service.in.AliyunService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2024/10/10 16:00
 */
@Component
@Slf4j
public class AliyunServiceImpl implements AliyunService {

    @Autowired
    private IceConfig iceConfig;
    private IAcsClient ossRamClient;
    private OSS ossClient;

    @PostConstruct
    public void init() throws Exception {
        IClientProfile ramProfile = DefaultProfile.getProfile(iceConfig.getAliyunDefaultOssRegion(),
                iceConfig.getAliyunOssAccessKeyId(), iceConfig.getAliyunOssAccessKeySecret());
        ossRamClient = new DefaultAcsClient(ramProfile);
        ossClient = new OSSClientBuilder().build(iceConfig.getAliyunOssEndpoint(),
                iceConfig.getAliyunOssAccessKeyId(), iceConfig.getAliyunOssAccessKeySecret());
    }

    @Override
    public AliyunCredentials getUploadCredentials(String domain, String bucket) {
        String policy = "{\"Version\":\"1\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"oss:PutObject\"],\"Resource\":[\"acs:oss:*:*:" + bucket + "/*\"]}]}";
        try {
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setRoleArn(iceConfig.getAliyunDefaultOssArn());
            request.setRoleSessionName(iceConfig.getAliyunDefaultOssRoleSessionName());
            request.setPolicy(policy);
            request.setDurationSeconds(900L);
            AssumeRoleResponse response = ossRamClient.getAcsResponse(request);
            return new AliyunCredentials(response.getCredentials().getAccessKeyId(),
                    response.getCredentials().getAccessKeySecret(),
                    bucket, iceConfig.getAliyunDefaultOssRegion(),
                    iceConfig.getAliyunOssEndpoint(),
                    response.getCredentials().getSecurityToken(),
                    response.getCredentials().getExpiration(),
                    bucket, domain);
        } catch (ClientException e) {
            log.error("generate oss upload AliyunCredentials fail!{},{},{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
        }
        return null;
    }

    @Override
    public String uploadFile(String filePath, String uploadName) {
        try {
            InputStream inputStream = new FileInputStream(filePath);
            ossClient.putObject(iceConfig.getAliyunDefaultBucket(),
                    uploadName, inputStream);
            return iceConfig.getAliyunDefaultDomain() + "/" + uploadName;
        } catch (Exception e) {
            log.error("上传文件报错:{},{}", filePath, uploadName, e);
        }
        return null;
    }
}
